<template>
  <div class='containerTags'>
    <el-card>
      <!-- 面包屑导航 -->
      <el-breadcrumb separator-class="el-icon-arrow-right" v-if="subject.id||subject.name">
        <el-breadcrumb-item :to="{ path: '/subjects/list' }" style="color:#000">学科管理</el-breadcrumb-item>
        <el-breadcrumb-item>{{subject.name}}</el-breadcrumb-item>
        <el-breadcrumb-item>目录管理</el-breadcrumb-item>
      </el-breadcrumb>
      <el-row>
        <el-col :span="20">
          <!-- 行内表单 -->
          <el-form :inline="true" :model="form" class="demo-form-inline">
            <el-form-item label="标签名称">
              <el-input v-model="form.tagName" placeholder="审批人"></el-input>
            </el-form-item>
            <el-form-item label="状态">
              <el-select v-model="form.state" placeholder="活动区域">
                <el-option v-for="item in status" :key="item.value" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button @click="clear">清除</el-button>
              <el-button type="primary" @click="search">搜索</el-button>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="2">
          <el-link type="primary" @click="goback" v-if="subject.id||subject.name">←返回学科</el-link>&nbsp;
        </el-col>
        <el-col :span="2">
          <el-button type="success" icon="el-icon-info" @click="add">新增目录</el-button>
        </el-col>
      </el-row>
      <!-- 数据总数 -->
      <el-alert
        :title="`数据一共${total}条`"
        type="info"
        show-icon
        :closable="false">
      </el-alert>
      <!-- 表格 -->
      <el-table
      :data="tableData"
      style="width: 100%">
      <el-table-column
        type="index"
        :index="indexMethod"
        label="序号">
      </el-table-column>
      <el-table-column
        prop="subjectName"
        label="所属学科">
      </el-table-column>
      <el-table-column
        prop="tagName"
        label="标签名称">
      </el-table-column>
      <el-table-column
        prop="username"
        label="创建者">
      </el-table-column>
      <el-table-column
        prop="addDate"
        label="创建日期">
        <template #default="{row}">{{row.addDate | parseTimeByString}}</template>
      </el-table-column>
      <el-table-column
        prop="state"
        label="状态">
        <template #default="{row}">{{status.find(item => item.value === row.state).label}}</template>
      </el-table-column>
      <el-table-column
        label="操作">
        <template #default="{row}">
          <el-link type="primary" v-text="row.state===1?'禁用':'启用'" @click="changeState(row.id,row.state)"></el-link>&nbsp;&nbsp;
          <el-link type="primary" :disabled="row.state===1" @click="edit(row.id)">修改</el-link>&nbsp;&nbsp;
          <el-link type="primary" :disabled="row.state===1" @click="del(row.id)">删除</el-link>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="10"
      layout="sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    </el-card>
    <tags-add ref="TagsAddRef" :dialogFormVisible.sync="dialogFormVisible" @reload="getTagsList"></tags-add>
  </div>
</template>

<script>
import { list, detail, changeState, remove } from '@/api/hmmm/tags.js'
import { status } from '@/api/hmmm/constants.js'
import TagsAdd from '../components/tags-add.vue'
export default {
  data () {
    return {
      form: {
        tagName: null,
        state: null,
        page: 1,
        pagesize: 10
      },
      status,
      total: 0,
      tableData: [],
      dialogFormVisible: false
    }
  },
  components: { TagsAdd },
  created () {
    this.getTagsList()
  },
  computed: {
    subject () {
      return this.$route.query || {}
    }
  },
  methods: {
    search () {
      // console.log('搜索')
      this.getTagsList()
    },
    clear () {
      // console.log('清除')
      this.form = {
        tagName: null,
        state: null,
        page: 1,
        pagesize: 10
      }
      this.getTagsList()
    },
    async getTagsList () {
      const { data } = await list({ ...this.form, subjectID: this.subject.id })
      console.log(data)
      this.tableData = data.items
      this.total = data.counts
    },
    handleSizeChange (val) {
      console.log(`每页 ${val} 条`)
      this.form.pagesize = val
      this.getTagsList()
    },
    handleCurrentChange (val) {
      console.log(`当前页: ${val}`)
      this.form.page = val
      this.getTagsList()
    },
    goback () {
      this.$router.go(-1)
    },
    add () {
      this.dialogFormVisible = true
      this.$refs.TagsAddRef.title = '新增标签'
    },
    indexMethod (index) {
      return (index + 1) + (this.form.page - 1) * this.form.pagesize
    },
    async edit (id) {
      const { data } = await detail({ id })
      this.$refs.TagsAddRef.form = data
      console.log(data)
      this.$refs.TagsAddRef.title = '修改标签'
      this.dialogFormVisible = true
    },
    async changeState (id, state) {
      const obj = { id }
      if (state === 1) {
        obj.state = 0
      } else {
        obj.state = 1
      }
      const res = await changeState(obj)
      console.log(res)
      this.getTagsList()
    },
    del (id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const res = await remove({ id })
        console.log(res)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        if (this.tableData.length === 1 && this.form.page > 1) {
          this.form.page = this.form.page - 1
        }
        this.getTagsList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style scoped lang='scss'>
.containerTags {
  margin: 20px;
}
::v-deep .el-breadcrumb__inner.is-link {
  color: #000
}
</style>
